package cn.tedu.charging.device.dao.repository.impl;

import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.po.GunInfoPO;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.dao.mapper.ChargingStationMapper;
import cn.tedu.charging.device.dao.mapper.DeviceGunMapper;
import cn.tedu.charging.device.dao.mapper.DevicePileMapper;
import cn.tedu.charging.device.dao.repository.DeviceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
public class DeviceRepositoryImpl implements DeviceRepository {
    @Autowired
    private DeviceGunMapper deviceGunMapper;
    @Autowired
    private DevicePileMapper devicePileMapper;
    @Autowired
    private ChargingStationMapper chargingStationMapper;
    @Override
    public Boolean updateGunStatus(GunStatusUpdateParam gunStatusUpdateParam) {
        log.debug("更新枪口状态，参数：{}", gunStatusUpdateParam);
        GunInfoPO gunInfoVO = new GunInfoPO();
        gunInfoVO.setId(gunStatusUpdateParam.getGunId());
        gunInfoVO.setGunStatus(gunStatusUpdateParam.getStatus());
        log.debug("封装参数:{}", gunInfoVO);
        int i = deviceGunMapper.updateById(gunInfoVO);
        log.debug("更新枪口状态参数:{},结果:{}",gunStatusUpdateParam, i);
        if (i==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public StationInfoVO getStationInfo(Integer gunId) {
        log.debug("获取场站信息，参数：{}", gunId);
        // 通过枪ID 获取 桩ID 通过桩ID 获取站ID  通过站ID 获取运营商ID
        // 1. 通过枪ID 获取 桩ID
        log.debug("通过枪ID 获取 桩ID,参数:{}", gunId);
        Integer pileId = deviceGunMapper.selectById(gunId).getPileId();
        log.debug("通过枪ID 获取 桩ID,参数:{},结果:{}", gunId, pileId);
        // 2. 通过桩ID 获取站ID
        log.debug("通过桩ID 获取站ID,参数:{}", pileId);
        Integer stationId = devicePileMapper.selectById(pileId).getStationId();
        log.debug("通过桩ID 获取站ID,参数:{},结果:{}", pileId, stationId);
        // 3. 通过站ID 获取运营商ID
        log.debug("通过站ID 获取运营商ID,参数:{}", stationId);
        Integer operatorId = chargingStationMapper.selectById(stationId).getOperatorId();
        log.debug("通过站ID 获取运营商ID,参数:{},结果:{}", stationId, operatorId);

        //
        StationInfoVO stationInfoVO = new StationInfoVO();
        // 设置运营商ID
        stationInfoVO.setOperatorId(operatorId);
        // 设置枪ID
        stationInfoVO.setGunId(gunId);
        // 设置桩ID
        stationInfoVO.setPileId(pileId);
        // 设置站ID
        stationInfoVO.setId(stationId);
        log.debug("封装结果:{}", stationInfoVO);
        return stationInfoVO;
    }
}
